#include "All-riderRegister.h"

RiderRegisterWindow::RiderRegisterWindow(database &db, int userId, int phoneId, QWidget *parent)
    : QDialog(parent), db(db), userId(userId), phoneId(phoneId)
{
    setWindowTitle("骑手注册");
    setFixedSize(800, 650);
    
    // 设置窗口标志，使其成为对话框窗口
    setWindowFlags(Qt::Dialog | Qt::WindowTitleHint | Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);
    
    // 设置窗口位置居中
    if (parent) {
        move(parent->window()->frameGeometry().center() - frameGeometry().center());
    }
    
    setupUI();
}

RiderRegisterWindow::~RiderRegisterWindow()
{
}

void RiderRegisterWindow::setupUI()
{
    // 创建主布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setSpacing(15);
    mainLayout->setContentsMargins(20, 20, 20, 20);

    // 创建标题
    QLabel *titleLabel = new QLabel("骑手注册申请", this);
    titleLabel->setStyleSheet("font-size: 20pt; font-weight: bold; margin-bottom: 20px;");
    titleLabel->setAlignment(Qt::AlignCenter);
    mainLayout->addWidget(titleLabel);

    // 创建表单布局
    QFormLayout *formLayout = new QFormLayout();
    formLayout->setSpacing(15);
    formLayout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
    formLayout->setLabelAlignment(Qt::AlignRight | Qt::AlignVCenter);

    // 样式
    QString labelStyle = "font-size: 14pt; font-weight: bold; color: #333;";
    QString inputStyle = "font-size: 14pt; padding: 8px; border: 1px solid #ccc; border-radius: 4px;";

    // 用户ID显示（不可编辑）
    QLabel *userIdLabel = new QLabel(QString::number(userId), this);
    userIdLabel->setStyleSheet("font-size: 14pt; padding: 8px;");
    QLabel *userIdTitleLabel = new QLabel("用户ID:", this);
    userIdTitleLabel->setStyleSheet(labelStyle);
    formLayout->addRow(userIdTitleLabel, userIdLabel);

    // 骑手姓名
    riderNameEdit = new QLineEdit(this);
    riderNameEdit->setStyleSheet(inputStyle);
    riderNameEdit->setPlaceholderText("请输入真实姓名");
    QLabel *riderNameLabel = new QLabel("骑手姓名:", this);
    riderNameLabel->setStyleSheet(labelStyle);
    formLayout->addRow(riderNameLabel, riderNameEdit);

    // 联系电话
    phoneEdit = new QLineEdit(this);
    phoneEdit->setStyleSheet(inputStyle);
    phoneEdit->setPlaceholderText("请输入联系电话");
    QLabel *phoneLabel = new QLabel("联系电话:", this);
    phoneLabel->setStyleSheet(labelStyle);
    formLayout->addRow(phoneLabel, phoneEdit);

    mainLayout->addLayout(formLayout);

    // 添加说明文本
    QLabel *noteLabel = new QLabel("注意：提交申请后，管理员将会审核您的信息。\n审核通过后，您将成为平台骑手。", this);
    noteLabel->setStyleSheet("font-size: 12pt; color: #666; margin-top: 10px;");
    noteLabel->setAlignment(Qt::AlignCenter);
    mainLayout->addWidget(noteLabel);

    // 添加按钮布局
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->setSpacing(15);

    registerButton = new QPushButton("提交申请", this);
    registerButton->setStyleSheet("font-size: 14pt; padding: 10px 20px; border-radius: 4px; background-color: #4CAF50; color: white;");
    connect(registerButton, &QPushButton::clicked, this, &RiderRegisterWindow::onRegisterClicked);

    cancelButton = new QPushButton("取消", this);
    cancelButton->setStyleSheet("font-size: 14pt; padding: 10px 20px; border-radius: 4px;");
    connect(cancelButton, &QPushButton::clicked, this, &RiderRegisterWindow::onCancelClicked);

    buttonLayout->addStretch();
    buttonLayout->addWidget(registerButton);
    buttonLayout->addWidget(cancelButton);
    buttonLayout->addStretch();

    mainLayout->addLayout(buttonLayout);
    
    // 检查是否已有申请记录
    QString sql = QString("select name_object, phone_id, phone from review where user_id = %1 and identity_object = 3").arg(userId);
    QSqlQuery query = db.exec(sql);
    if (query.next()) {
        // 已有申请记录，显示已有信息并禁用编辑
        riderNameEdit->setText(query.value(0).toString());
        riderNameEdit->setReadOnly(true);
        phoneEdit->setText(query.value(2).toString()); // 使用 phone 字段
        phoneEdit->setReadOnly(true);
        registerButton->setStyleSheet("font-size: 14pt; padding: 10px 20px; border-radius: 4px; background-color: #d4d6d5; color: white;");
        registerButton->setEnabled(false);
    } else {
        // 填充初始电话
        sql = QString("select phone from phone where phone_id = %1").arg(phoneId);
        query = db.exec(sql);
        if (query.next()) {
            defaultPhone = query.value(0).toString();
            phoneEdit->setText(defaultPhone);
        }
    }
}

void RiderRegisterWindow::onRegisterClicked()
{
    // 获取表单数据
    QString riderName = riderNameEdit->text().trimmed();
    QString phone = phoneEdit->text().trimmed();
    
    // 验证数据
    if (riderName.isEmpty()) {
        QMessageBox::warning(this, "提示", "请输入骑手姓名");
        return;
    }
    
    if (phone.isEmpty()) {
        QMessageBox::warning(this, "提示", "请输入联系电话");
        return;
    }
    
    // 调试输出
    qDebug() << userId << " " << riderName << " " << phone;
    
    // 检查手机号是否变更

        // 提交申请到review表
        QString sql = QString("insert into review(user_id, review_status, identity_object, name_object, phone_id, phone) "
                             "values(%1, 0, 3, '%2', '%3', '%4')").arg(userId).arg(riderName).arg(phoneId).arg(phone);
        qDebug() << sql;
        db.exec(sql);

    if (defaultPhone == phone) {
        QMessageBox::information(this, "提示", "手机号未变更\n骑手注册申请已发送至管理员后台\n请耐心等待");
    } else {
        QMessageBox::information(this, "提示", "骑手注册申请已发送至管理员后台\n请耐心等待");
        }
    accept();
}

void RiderRegisterWindow::onCancelClicked()
{
    reject();
} 